﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfumeShopManagement.DAL;
using PerfumeShopManagement.Utility;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.BLL;

namespace PerfumeShopManagement.View
{
    public partial class frmProductManagement : Form
    {
        BllProduct bllProduct = new BllProduct();
        BllCategory bllCategory = new BllCategory();

        public frmProductManagement()
        {
            InitializeComponent();
        }

        private void resetFieldWithoutId()
        {
            // Reset without id and inventory
            txbProductName.Text = "";
            txbProductDetail.Text = "";
            txbPrice.Text = "";
            txbProductDetail.Text = "";
        }

        private void setEnableInventory(bool enable)
        {
            this.txbInventory.Enabled = enable;
            if (enable)
                this.txbInventory.Text = "";
        }

        private void setNextId()
        {
            txbProductId.Text = Constants.AUTO_NUMBERING_MESSAGE;
            setEnableInventory(true);
        }

        private void reloadCategoryList()
        {
            List<Category> categories = bllCategory.GetAll();
            cbbCategory.Items.Clear();
            foreach (Category category in categories)
            {
                cbbCategory.Items.Add(category.CategoryName);
            }
        }

        private void reloadGridView()
        {
            // Load grid view
            object[] buffer = new object[6];
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            List<Product> products = bllProduct.GetAll();
            foreach (Product item in products)
            {
                buffer[0] = item.ProductId;
                buffer[1] = item.ProductName;
                buffer[2] = item.Price;
                buffer[3] = item.Inventory;
                buffer[4] = item.DetailInfo;
                buffer[5] = item.Category.CategoryName;

                rows.Add(new DataGridViewRow());
                rows[rows.Count - 1].CreateCells(gridProducts, buffer);
            }
            gridProducts.Rows.Clear();
            gridProducts.Rows.AddRange(rows.ToArray());
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            setNextId();
            resetFieldWithoutId();
        }

        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            if (gridProducts.SelectedRows.Count > 0)
            {
                bool allDeleted = true;
                foreach (DataGridViewRow item in gridProducts.SelectedRows)
                {
                    int id = (int)item.Cells["colProductId"].Value;
                    if (!bllProduct.DeleteById(id))
                        allDeleted = false;
                }

                if (allDeleted)
                {
                    reloadGridView();
                    MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
                }
                else
                {
                    MessageBox.Show(Constants.FAILED_MESSAGE);
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            resetFieldWithoutId();
        }

        private void btnSaveProduct_Click(object sender, EventArgs e)
        {
            Product product = new Product();
                if (NumberUtil.IsNumeric(txbProductId.Text))
                    product.ProductId = int.Parse(txbProductId.Text);

                product.ProductName = txbProductName.Text;

                if (NumberUtil.IsNumeric(txbPrice.Text))
                    product.Price = double.Parse(txbPrice.Text);

                if (NumberUtil.IsNumeric(txbInventory.Text))
                    product.Inventory = int.Parse(txbInventory.Text);

                product.DetailInfo = txbProductDetail.Text;

                Category criteria = new Category();
                criteria.CategoryName = cbbCategory.Text;
                List<Category> categories = bllCategory.Search(criteria);
                if (categories.Count > 0)
                {
                    product.CategoryId = ((Category)categories[0]).CategoryId;
                }

            if (bllProduct.Save(product))
            {
                reloadGridView();
                MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
            }
            else
            {
                MessageBox.Show(Constants.FAILED_MESSAGE);
            }
        }

        private void frmProductManagement_Load(object sender, EventArgs e)
        {
            setNextId();
            resetFieldWithoutId();
            reloadCategoryList();
            reloadGridView();
        }

        private void btnEditProduct_Click(object sender, EventArgs e)
        {
            if (gridProducts.SelectedRows.Count > 0)
            {
                DataGridViewRow item = gridProducts.SelectedRows[0];
                txbProductId.Text = ((int)item.Cells["colProductId"].Value).ToString();
                txbProductName.Text = (string)item.Cells["colProductName"].Value;
                txbPrice.Text = ((double)item.Cells["colPrice"].Value).ToString();
                txbInventory.Text = ((int)item.Cells["colInventory"].Value).ToString();
                txbProductDetail.Text = (string)item.Cells["colProductDetail"].Value;
                
                cbbCategory.Text = "";
                Category criteria = new Category();
                criteria.CategoryName = (string)item.Cells["colCategoryName"].Value;

                List<Category> result = bllCategory.Search(criteria);
                if (result.Count > 0)
                    cbbCategory.Text = result[0].CategoryName.ToString();
                setEnableInventory(false);
            }
        }

        private void btnAddNewCategory_Click(object sender, EventArgs e)
        {
            frmCategoryManagement categoryManagementView = new frmCategoryManagement(bllCategory);
            categoryManagementView.Visible = true;
            categoryManagementView.FormClosed += new FormClosedEventHandler(categoryManagementView_FormClosed);
            this.Enabled = false;
        }

        void categoryManagementView_FormClosed(object sender, FormClosedEventArgs e)
        {
            reloadCategoryList();
            this.Enabled = true;
        }
    }
}
